import { delTags, getTags, putTags } from '@/services';
import { ITagsItem } from '@/types';
import { Effect, Reducer } from 'umi';
export interface TagsModelState {
  tagsDetail: Partial<ITagsItem>;
  tagsAdd: Partial<ITagsItem>;
  tagsDel: Partial<ITagsItem>;
}
export interface TagsModelType {
  namespace: 'tags';
  state: TagsModelState;
  effects: {
    getTags: Effect;
    putTags: Effect;
    delTags: Effect;
  };
  reducers: {
    save: Reducer<TagsModelState>;
  };
}
const EditorModel: TagsModelType = {
  namespace: 'tags',
  state: {
    tagsDetail: {},
    tagsAdd: {},
    tagsDel: {},
  },
  effects: {
    *getTags({ payload }, { call, put }) {
      let result = yield call(
        getTags,
        payload.id,
        payload.label,
        payload.value,
      );
      if (result.data) {
        yield put({
          type: 'save',
          payload: {
            tagsDetail: result.data,
          },
        });
      }
    },
    *putTags({ payload }, { call, put }) {
      let result = yield call(putTags, payload.label, payload.value);
      if (result.data) {
        yield put({
          type: 'save',
          payload: {
            tagsAdd: result.data,
          },
        });
      }
    },
    *delTags({ payload }, { call, put }) {
      let result = yield call(delTags, payload.id);
      if (result.data) {
        yield put({
          type: 'save',
          payload: {
            tagsDel: result.data,
          },
        });
      }
    },
  },

  reducers: {
    save(state, action) {
      return {
        ...state,
        ...action.payload,
      };
    },
  },
};

export default EditorModel;
